package com.hmall.cart.listener;

import com.hmall.api.dto.ClearCartMessageDTO;
import com.hmall.cart.service.ICartService;
import com.hmall.common.constants.MqConstants;
import com.hmall.common.utils.UserContext;
import org.checkerframework.checker.units.qual.C;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * @author 16701
 */
@Component
public class CartClearListener {
    @Autowired
    private ICartService cartService;

    @RabbitListener(bindings = @QueueBinding(
            value=@Queue(value= MqConstants.CART_CLEAR_QUEUE),
            exchange = @Exchange(value=MqConstants.ORDER_CREATE_EXCHANGE_NAME),
            key = MqConstants.ORDER_CREATE_KEY
    ))


    public void cartClearListen(ClearCartMessageDTO dto){
        Long userId = dto.getUserId();
        Set<Long> itemIds = dto.getItemIds();
        UserContext.setUser(userId);
        cartService.removeByItemIds(itemIds);
        UserContext.removeUser();
    }

}
